package onlineclass.service;



import onlineclass.dao.OResourceMapper;
import onlineclass.dao.OVideoMapper;
import onlineclass.entity.OResource;
import onlineclass.entity.OVideo;
import onlineclass.util.MyBatisUtil;
import onlineclass.util.AjaxPage;
import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class OVideoService {
    private static Logger Log = LoggerFactory.getLogger(OVideoService.class);
    public static final OVideoService INST = new OVideoService();
    public AjaxPage search(int pageNo, int pageSize,String title){
        SqlSession s = MyBatisUtil.get();
        OVideoMapper mapper = s.getMapper(OVideoMapper.class);
        try {
            List<OVideo> resource = mapper.search(pageNo,10,title);
            int total = mapper.searchTotal(title);
            return  new AjaxPage(resource,total,pageSize,pageNo);
        }catch (Exception e){
            System.out.println(e.getMessage());
            return null;
        }finally {
            MyBatisUtil.close(s);
        }
    }
    public OVideo get(long id){
        SqlSession s = MyBatisUtil.get();
        OVideoMapper mapper = s.getMapper(OVideoMapper.class);
        try {
            return  mapper.get(id);
        }catch (Exception e){
            System.out.println(e.getMessage());
            return null;
        }finally {
            MyBatisUtil.close(s);
        }
    }

    public int add(OVideo p){
        SqlSession s = MyBatisUtil.get();
        OVideoMapper mapper = s.getMapper(OVideoMapper.class);
        try {
            return  mapper.add(p);
        } finally {
            MyBatisUtil.close(s);
        }
    }
    public int update(OVideo p){
        SqlSession s = MyBatisUtil.get();
        OVideoMapper mapper = s.getMapper(OVideoMapper.class);
        try {
            return  mapper.update(p);
        }catch (Exception e){
            System.out.println(e.getMessage());
            return -1;
        }finally {
            MyBatisUtil.close(s);
        }
    }
    public int del(long id){
        SqlSession s = MyBatisUtil.get();
        OVideoMapper mapper = s.getMapper(OVideoMapper.class);
        try {
            return  mapper.del(id);
        }catch (Exception e){
            System.out.println(e.getMessage());
            return -1;
        }finally {
            MyBatisUtil.close(s);
        }
    }


}
